//
//  WFHomeViewController.m
//  WisdomFarm
//
//  Created by sky on 2017/10/24.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFHomeViewController.h"
#import "UINavigationController+WFUtils.h"
#import "WFLocationManager.h"
#import "WFAgricultureUploadViewController.h"
#import "WFDisasterUploadViewController.h"

@interface WFHomeViewController ()

@end

@implementation WFHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.url = [NSURL URLWithString:@"http://www.zjmb.gov.cn/zhnyqx/index.html"];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webView:(UIWebView *)webView didCreateJavaScriptContext:(JSContext *)ctx {
    [super webView:webView didCreateJavaScriptContext:ctx];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (![WFStore currentFarm]) {
            WFFarm *farm = [WFFarm new];
            [SVProgressHUD showWithStatus:@"正在定位,请稍候..."];
            [[WFLocationManager sharedManager] startUpdateLocation:^(CLLocation *result) {
                if (!result) {
                    farm.latitude = 30;
                    farm.longitude = 120;
                } else {
                    farm.latitude = result.coordinate.latitude;
                    farm.longitude = result.coordinate.longitude;
                }
                [[WFLocationManager sharedManager] startUpdateLocationForAddress:^(NSString *address) {
                    farm.name = address ?: @"";
                    [self setFarmForHTML:farm];
                    [SVProgressHUD dismiss];
                }];
            }];
        } else {
            WFFarm *currentFarm = [WFStore currentFarm];
            [self setFarmForHTML:currentFarm];
        }

    });
}

- (void)navigateSelectFarm {
    mz_dispatch_on_main_queue(^{
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        WFFarm *currentFarm = [WFStore currentFarm];
        if (currentFarm.id) {
            [params setObject:currentFarm.id forKey:@"farmId"];
        }
        @weakify(self);
        void (^completion)(WFFarm *) = ^(WFFarm *farm) {
            @strongify(self);
            [WFStore setCurrentFarm:farm];
            [self setFarmForHTML:farm];
            [self dismissViewControllerAnimated:YES completion:nil];
        };
        [params setObject:completion forKey:@"farmClickHandle"];
        [WFRouter toFarmListWithParams:params];
    });
}

- (void)setFarmForHTML:(WFFarm *)farm {
    NSDictionary *farmDic = @{
                              @"address" : farm.name,
                              @"lat" : @(farm.latitude),
                              @"lon" : @(farm.longitude)
                              };
    NSString *farmStr = [farmDic yy_modelToJSONString];
    NSString *js = [NSString stringWithFormat:@"setLocation('%@')", farmStr];
    [self.webview.sc_jsContext evaluateScript:js];
}

@end
